home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / INTERVIE / GRAPHIC / OBJMAN.H < prev    next >
C/C++ Source or Header  |  1991-12-10  |  5KB  |  139 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Interface to ObjectMan (object manager) class.
  25.  */
  26.  
  27. #ifndef objectman_h
  28. #define objectman_h
  29.  
  30. #include <InterViews/defs.h>
  31. #include <InterViews/Graphic/persistent.h>
  32.  
  33. #define INVALIDOFFSET 1
  34. #define NAMESIZE 100
  35.  
  36. typedef long ObjOffset;
  37.  
  38. extern char* OBJMAP_POSTFIX;
  39. extern char* OBJSTORE_POSTFIX;
  40.  
  41. class Cache;
  42. class PFile;
  43. class Ref;
  44. class RefList;
  45.  
  46. class ObjectMan : public Persistent {
  47. public:
  48.     ObjectMan();
  49.     ObjectMan(
  50.     char* filename, 
  51.     void (*userInitializer)(RefList*) =nil,
  52.     Persistent* (*userCreator)(ClassId) =nil
  53.     );
  54.     ~ObjectMan();
  55.  
  56.     /* 
  57.      * The parameter userInitializer must point to a function that creates
  58.      * primordial root objects needed by your application, and installs
  59.      * them in the passed RefList.  It will be called only when the
  60.      * application is run for the FIRST TIME.  Henceforth, such objects
  61.      * will be read from the persistent object store.
  62.      *
  63.      * The parameter userCreator must point to a function that can create
  64.      * an instance of ANY persistent object used in the application, given
  65.      * a ClassId.  Typically it will deal explicitly with Classes derived
  66.      * specifically for this application, and call library-defined
  67.      * functions for Classes taken from libraries.
  68.      */
  69.     
  70.     RefList* GetRoot();
  71.     Persistent* Create(ClassId id);
  72.     UID GetUID(Persistent*);
  73.     /* returns a unique UID for the Persistent */
  74.     /* looks in cache first, allocates new UID if not found */
  75.     
  76.     boolean Invalidate(Persistent*);
  77.     /* sets the objStore offset associated with the object in the */
  78.     /* objMap to INVALIDOFFSET; useful when deallocating an object */
  79.     /* once and for all */
  80.  
  81.     boolean IsCached(Ref*);
  82.     /* looks in the cache for the referenced object; returns false if */
  83.     /* not found, true otherwise.  Puts the object into Ref as a side */
  84.     /* effect */
  85.     boolean Find(Ref*);
  86.     /* put Persistent * into Ref, maybe seeking for and reading */
  87.     /* Persistent */
  88.     /* from disk and return true if all went well */
  89.     boolean Retrieve(Ref*);
  90.     /* reads Persistent blindly from disk (no seek) and return true */
  91.     /* if all went well.  Assumes object referred to by *ref is */
  92.     /* NOT in memory.  Used when reading clustered objects, */
  93.     /* for which no offset exists in the objMap since clustered objects */
  94.     /* are written consecutively */
  95.  
  96.     boolean Store(Persistent*);        /* find new (disk) space for Persistent */
  97.     boolean Update(Persistent*);    /* update objMap for Persistent */
  98.  
  99.     boolean ObjectIsDirty(Persistent*);
  100.     void ObjectTouch(Persistent*);
  101.     void ObjectClean(Persistent*);
  102.  
  103.     virtual ClassId GetClassId();
  104.     virtual boolean IsA(ClassId);
  105. protected:
  106.     void (*userInitializer)(RefList*);
  107.     Persistent* (*userCreator)(ClassId id);
  108.  
  109.     ObjOffset getOffset(UID);
  110.     boolean seek(UID uid);    /* moves to proper position in objStore */
  111.     UID nextUID();
  112.  
  113.     virtual boolean read(PFile*);
  114.     virtual boolean write(PFile*);
  115. protected:
  116.     UID lastuid;        /* last UID I allocated */
  117.     ObjOffset lastuidOffset;    /* spot where lastuid is written in objStore */
  118.     ObjOffset currentOffset;    /* offset of last Persistent calling Write */
  119.  
  120.     char filename [NAMESIZE];   /* we'll gag if names get too big */
  121.     Cache* objCache;
  122.     PFile* objMap, *objStore;
  123.  
  124.     RefList* root;
  125. };
  126.  
  127. /*
  128.  * inlines
  129.  */
  130.  
  131. inline RefList* ObjectMan::GetRoot () { return root; }
  132. inline UID ObjectMan::nextUID () { return lastuid += 2; }
  133.  
  134. inline ObjOffset ObjectMan::getOffset (UID uid) { 
  135.     return (uid >> 1) * sizeof(ObjOffset);
  136. }
  137.  
  138. #endif
  139.